package com.jinke.uang.camera;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jinke.uang.R;
import com.jinke.uang.base.BaseActivity;
import com.jinke.uang.interfaces.ILoading;
import com.jinke.uang.utils.FileUtils;
import com.otaliastudios.cameraview.BitmapCallback;
import com.otaliastudios.cameraview.FileCallback;
import com.otaliastudios.cameraview.PictureResult;

import java.io.File;
import java.lang.ref.WeakReference;

import androidx.annotation.Nullable;
import crossoverone.statuslib.StatusUtil;

/**
  *  @author lixiaonan
  *  功能描述: 图片展示的
  *  时 间： 2019/4/10 下午6:34
  */
public class PicturePreviewActivity extends BaseActivity implements View.OnClickListener {
    private LinearLayout ll_bottom;
    private TextView tv_confirm;
    private TextView tv_cancel;
    private static WeakReference<PictureResult> image;
    private static Bitmap bitmap;

    public static void setPictureResult(@Nullable PictureResult im) {
        image = im != null ? new WeakReference<>(im) : null;
    }

    public static byte[] getImage() {
        return image!=null?image.get().getData():null;
    }

    public static Bitmap getBitmap() {
        return bitmap;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusUtil.setUseStatusBarColor(this, Color.parseColor("#00000000"));
        // 第二个参数是是否沉浸,第三个参数是状态栏字体是否为黑色。
        StatusUtil.setSystemStatus(this, true, false);

        setContentView(R.layout.activity_picture_preview);
        ll_bottom = findViewById(R.id.ll_bottom);
        tv_confirm = findViewById(R.id.tv_confirm);
        tv_cancel = findViewById(R.id.tv_cancel);
        tv_confirm.setOnClickListener(this);
        tv_cancel.setOnClickListener(this);
        final ImageView imageView = findViewById(R.id.image);


        PictureResult result = image == null ? null : image.get();
        if (result == null) {
            finish();
            return;
        }
        result.toBitmap(1000, 1000, new BitmapCallback() {
            @Override
            public void onBitmapReady(@Nullable Bitmap bitmapYes) {
                bitmap = bitmapYes;
                imageView.setImageBitmap(bitmapYes);
                ll_bottom.setVisibility(View.VISIBLE);
            }
        });
        if (result.isSnapshot()) {
            // Log the real size for debugging reason.
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            bitmap=BitmapFactory.decodeByteArray(result.getData(), 0, result.getData().length, options);
            if (result.getRotation() % 180 != 0) {
                Log.e("PicturePreview", "The picture full size is " + result.getSize().getHeight() + "x" + result.getSize().getWidth());
            } else {
                Log.e("PicturePreview", "The picture full size is " + result.getSize().getWidth() + "x" + result.getSize().getHeight());
            }
        }
    }

    @Override
    protected ILoading createLoadingView() {
        return null;
    }

    @Override
    protected int getResId() {
        return 0;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_confirm:
                image.get().toFile(FileUtils.getPicPath(), new FileCallback() {
                    @Override
                    public void onFileReady(@Nullable File file) {
                        Intent intent = new Intent();
                        intent.putExtra("filename", file.getAbsolutePath());
                        PicturePreviewActivity.this.setResult(0x01, intent);
                        PicturePreviewActivity.this.finish();
                    }
                });
                break;
            case R.id.tv_cancel:
                setResult(0x02);
                finish();
                break;
            default:
                break;
        }

    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (!isChangingConfigurations()) {
            setPictureResult(null);
        }
    }
}